<?php

namespace app\api\service;

use app\common\model\Brand;
use app\common\model\Category;
use app\common\model\Evaluation;
use app\common\model\Product;
use app\common\model\User;
use app\Response;

class ProductService
{
    /*** 产品列表
     * @param $page int 页码
     * @param $num int 个数
    */
    public static function productList($user_id,$page,$num,$key,$type,$class_one,$class_two,$price_sort,$commission_sort,$fs_up_status,$brand_up_status,$pre_up_status,$br_id,$material){
        $where[] = ["enable","=",1];
        $where[] = ["state","=",5];
        if(!empty($key)){
            $where[] = ["p_num|p_name","like","%".$key."%"];
        }
        if(!empty($class_one)){
            $where[] = ["class_one","=",$class_one];
        }
        if(!empty($class_two)){
            $where[] = ["class_two","=",$class_two];
        }
        $orderBy = "id desc";
        if($type == 1){
            $where[] = ["brand_state","=",1];
        }elseif($type == 2){
            $where[] = ["fs_state","=",1];
        }elseif($type == 3){
            $where[] = ["pre_state","=",1];
        }
        if(!empty($price_sort)){
            if($price_sort == 1){
                $orderBy = "supply_price asc";
            }elseif($price_sort == 2){
                $orderBy = "supply_price desc";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $orderBy = "supply_price desc";
            }
        }
        if(!empty($commission_sort)){
            if($commission_sort == 1){
                $orderBy = "commission_price,commission_rate asc";
            }elseif($commission_sort == 2){
                $orderBy = "commission_price,commission_rate desc";
            }
        }
        if(!empty($fs_up_status)&&$fs_up_status!=-1){
            $where[] = ["fs_up_status","=",$fs_up_status];
            $where[] = ["fs_up_time","<",time()];
            $orderBy = "fs_up_rank desc";
        }elseif($fs_up_status == -1){
            $where[] = ["fs_up_status","=",0];
        }
        if(!empty($brand_up_status)&&$brand_up_status!=-1){
            $where[] = ["brand_up_status","=",$brand_up_status];
            $where[] = ["brand_up_time","<",time()];
            $orderBy = "brand_up_rank desc";
        }elseif($brand_up_status == -1){
            $where[] = ["brand_up_status","=",0];
        }
        if(!empty($pre_up_status)&&$pre_up_status!=-1){
            $where[] = ["pre_up_status","=",$pre_up_status];
            $where[] = ["pre_up_time","<",time()];
            $orderBy = "pre_up_rank desc";
        }elseif($pre_up_status == -1){ 
            $where[] = ["pre_up_status","=",0];
        }
        if(is_numeric($br_id)){
            $where[] = ["br_id","=",$br_id];
        }
        if($material == 1){
            $where[] = ["img_json","<>",""];
        }
        $where[] = ["tie_up_pid","=",0];
        $p_field = "id,p_num,p_name,img,img_json,det_info,specifications,details_img_json,supply_price,commission_rate,commission_price,shi_type,vir_sales,sold";
        $data = Product::dataList($where,$page,$num,$p_field,$orderBy);
        if (is_numeric($user_id)) {
            $userInfo = User::userInfo(["uid"=>$user_id]);
        }
        if(!empty($data)){
            $data = $data->toArray();
            foreach ($data as $k => $v){
                $data[$k]["img_json"] = !empty($v["img_json"])?json_decode($v["img_json"]):"";
                $data[$k]["details_img_json"] = !empty($v["details_img_json"])?json_decode($v["details_img_json"]):"";
                if(!empty($userInfo)&&$userInfo["commission_rate"]){
                    $commission_price = bcsub($v["commission_price"],4,0);
                    $data[$k]["commission_price"] = bcmul($commission_price,$userInfo["commission_rate"]/100,2);
                }
            }
            $count= Product::dataCount($where);
        }else{
            $data = [];
            $count = 0;
        }
        return [$data,$count];
    }

    /*** 产品详情
     * @param $id int 产品ID
     * @param $num int 个数
     */
    public static function productDetailed($user_id,$id){
        $info = [];
        $data = Product::dataInfo(["id"=>$id]);
        $info = $data;
        if (is_numeric($user_id)) {
            $userInfo = User::userInfo(["uid"=>$user_id]);
        }
        $info["img_json"] = !empty($info["img_json"])?json_decode($info["img_json"]):"";
        $info["details_img_json"] = !empty($info["details_img_json"])?json_decode($info["details_img_json"]):"";
        if(!empty($userInfo)&&$userInfo["commission_rate"]){
            $commission_price = bcsub($info["commission_price"],4,0);
            $info["commission_price"] = bcmul($commission_price,$userInfo["commission_rate"]/100,2);
        }
        $info["pre_sale_time"] = !empty($info["pre_sale_time"])?date("Y-m-d H:i:s",$info["pre_sale_time"]):"";
        $sku_info = Product::dataListAll(["tie_up_pid"=>$id,"enable"=>1,"state"=>5],"*","id asc");
        $info["sku_info"] = !empty($sku_info)?$sku_info->toArray():array();
        return $info;
    }



    public static function productEvaluateList($id,$page,$num,$order_type,$sku_id){
        $where[] = ["e.enable","=",1];
        $where[] = ["e.state","=",2];
        if(is_numeric($id)&&$id!=0){
            $where[] = ["e.pid","=",$id];
        }
        if(is_numeric($sku_id)&&$sku_id!=0){
            $where[] = ["e.sku_id","=",$sku_id];
        }
        $field = "e.*,u.nickname,u.photo,p.img,p.supply_price,p.cost_price";
        if ($order_type == 1){
            $sort = "e.addtime asc";
        }elseif ($order_type == 2){
            $sort = "e.addtime desc";
        }
        $list = Evaluation::evaluateList($where,$page,$num,$field,$sort);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $k => $v){
                $list[$k]["addtime"] = !empty($v["addtime"])?date("Y.m.d"):"";
                $list[$k]["img_json"] = !empty($v["img_json"])?json_decode($v["img_json"]):"";
            }
            $count = Evaluation::evaluateListCount($where);
        }else{
            $list = [];
            $count= 0;
        }
        return [$list,$count];
    }

    /*** 品牌列表
     * @param $id int 产品ID
     * @param $num int 个数
     */
    public static function productBrandList($page,$num){
        $where[] = ["enable","=",1];
        $info = Brand::dataList($where,$page,$num);
        return Response::wrapData($info);
    }

    /*** 分类列表
     * @param $id int 产品ID
     * @param $num int 个数
     */
    public static function categoryList($page,$num,$superior_id){
        $where[] = ["enable","=",1];
        if(is_numeric($superior_id)){
            $where[] = ["superior_id","=",$superior_id];
        }
        $list = Category::dataList($where,$page,$num,"*","id asc");
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $k => $v){
                $listCategory_two = Category::dataListAll(["enable"=>1,"superior_id"=>$v["id"]],"*","id asc");
                foreach ($listCategory_two as $k2 => $v2) {
                    $list[$k]['child'][] = array("id"=>$v2['id'],"superior_id"=>$v['id'],"category"=>$v2['category']);
                }
            }
        }

        return Response::wrapData($list);
    }
}